package com.louzhu.gulimall.search.controller;

import com.louzhu.gulimall.search.service.MallSearchService;
import com.louzhu.gulimall.search.vo.SearchParam;
import com.louzhu.gulimall.search.vo.SearchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class SearchController {

    @Autowired
    private MallSearchService mallSearchService;

    /**
     * 检索请求
     * spring MVC 自动将请求参数封装在 SearchParam searchParam 中
     */
    @GetMapping("/list.html")
    public String listPage(SearchParam searchParam, Model model, HttpServletRequest request){
        // 获取String类型的请求参数
        String queryString = request.getQueryString();
        searchParam.set_queryString(queryString);
        // 根据页面请求参数从ES中查询出页面需要的数据
        SearchResult searchResult = mallSearchService.search(searchParam);
        System.out.println("查询结果：" + searchResult.toString());
        System.out.println(searchResult.getPageNavs());
        System.out.println(searchResult.getTotalPages());
        System.out.println(searchResult.getTotal());
        System.out.println(searchResult.getPageNum());
        // 将数据渲染到页面
        model.addAttribute("result",searchResult);
        // 转发到检索页面
        return "list";
    }
}
